//用户的增删查 (登录注册)
const conn = require("../../utils/sql")
const resFormat = require("../../model/responseFormat")
// const redis = require("../../redis")
const jwt = require("../../utils/jwt")
const User = {}

User.login = (req,res)=>{
  /**
* @swagger
* /user/login:
*   post:
*     summary: 登录
*     tags: [User]
*     responses:
*       200:
*         description: success.
*         content:
*           application/json:
*             schema:
*               type: array
*               items:
*                 $ref: '#/User/login'
*/
  const userName = req.body.account
  const passWord = req.body.password
    const sql = `SELECT * FROM user WHERE userName= '${userName}' AND password='${passWord}'`;
    conn.query(sql, (err, r) => {
      if(err){
        console.log(err);
        res.end(JSON.stringify(resFormat.fai))
      }else if(r.length){
        const token = jwt.generate({username:userName},"10s")
        resFormat.suc.data = "登陆成功"
        res.header("Authorization",token)
        res.end(JSON.stringify(resFormat.suc))
      }else{
        resFormat.fai.msg = "账号或密码错误"
        res.end(JSON.stringify(resFormat.fai))
      }
    })
}
User.register = (req,res)=>{
  res.end(JSON.stringify(resFormat.suc))
}

module.exports = User